/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package filmlibrary.web;

import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import filmlibrary.orm.*;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author karol
 */
@ManagedBean(name="LoginManager")
@ApplicationScoped 
public class LoginManager {
    private static final Logger logger = Logger.getLogger("filmlibrary.web.LoginManager");
    private String login;
    private String pass;

    /** Creates a new instance of LoginManager */
    public LoginManager() {
        login = "";
        pass = "";
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
    public String logIn(){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        Query query = session.createQuery("from Users as u where u.login= :login and u.pass = :pass");
        /*query.setParameter("login", this.login);
        query.setParameter("pass", this.pass);
        List<Users> user = query.list();
        logger.info("size: "+user.size());
        if(user.size() == 1)
            return "/main.xhtml?faces-redirect=true";
        else{
	return "index.xhtml";
        }*/
        return "";
    }
    public String back(){
        return "/index.xhtml";
    }
    public void validateLogin(FacesContext facesContext, UIComponent uiComponent, Object value){
        logger.info("validowanie loginu");
        FacesContext context = FacesContext.getCurrentInstance();
        
        ResourceBundle rb = ResourceBundle.getBundle("filmlibrary.web.WebMessages", context.getViewRoot().getLocale());
        HtmlInputText htmlInputText = (HtmlInputText) uiComponent;
        String login = (String) value;
        if(login.length()<3 || login.length()>15){
            FacesMessage fm = new FacesMessage(htmlInputText.getLabel()+" "+rb.getString("ELength"));
            throw new ValidatorException(fm);
        }
    }
    public void validatePass(FacesContext facesContext, UIComponent uiComponent, Object value){
        FacesContext context = FacesContext.getCurrentInstance();
        ResourceBundle rb = ResourceBundle.getBundle("filmlibrary.web.WebMessages", context.getViewRoot().getLocale());
        HtmlInputSecret htmlInputSecret = (HtmlInputSecret) uiComponent;
        String pass = (String) value;
        if(pass.length()<5 || pass.length()>30){
            FacesMessage fm = new FacesMessage(htmlInputSecret.getLabel()+" "+rb.getString("ELength"));
            throw new ValidatorException(fm);
        }

    }
}
